//
//  HZVistorView.swift
//  555wobo
//
//  Created by 黄某某 on 15/12/16.
//  Copyright © 2015年 黄某某. All rights reserved.
//

import UIKit

protocol HZVistorViewDeleage : NSObjectProtocol{
    func vistorViewRegister()
    func vistorViewLogin()
}

class HZVistorView: UIView {
    weak var delegate : HZVistorViewDeleage?

    required init?(coder aDecoder: NSCoder){
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
            prepareUI()
        
    }
    func loginClick() {
        // 通知代理
        delegate?.vistorViewLogin()
    }
    func register() {
        delegate?.vistorViewRegister()
    }
    private func prepareUI(){
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints=false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        homeView.translatesAutoresizingMaskIntoConstraints=false
        messageLabel.translatesAutoresizingMaskIntoConstraints=false
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        loginButton.translatesAutoresizingMaskIntoConstraints=false
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier:1, constant: -40))
        
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier:1, constant: 0))
        
        
        self.addConstraint(NSLayoutConstraint(item:homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier:1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item:messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item:messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem:iconView, attribute: NSLayoutAttribute.Bottom, multiplier:1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:1, constant: 260))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier:1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:1, constant: 35))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier:1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier:1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:1, constant: 35))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:1, constant: 100))
        
        
    }
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var messageLabel : UILabel = {
        let lab = UILabel()
        lab.text = "关注周文杰,看看这里有什么惊喜!"
        lab.textColor = UIColor.darkGrayColor()
        lab.numberOfLines=0
        lab.sizeToFit()
        return lab
    }()
    private lazy var registerButton: UIButton = {
       let registerbtn = UIButton()
        registerbtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerbtn.setTitle("注册", forState: UIControlState.Normal)
        registerbtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        registerbtn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return registerbtn
    }()
    private lazy var loginButton: UIButton = {
        let loginbtn = UIButton()
        loginbtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginbtn.setTitle("登陆", forState: UIControlState.Normal)
        loginbtn.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
        loginbtn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return loginbtn
    }()
    
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion=false
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    func setupVistorView(imageName: String, message: String){
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        homeView.hidden = true
        coverView.hidden = true
        
    }

}
